package com.shop2cn.iapi.prodstockmgr.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.prodstockmgr.parameter.CatalogDto;
import com.shop2cn.iapi.prodstockmgr.parameter.mqStockOutBean;
import com.shop2cn.iapi.prodstockmgr.service.mqStockOutCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.database.model.stockmgr.agentinventory;
import com.ymttest.database.model.stockmgr.agentinventoryExample;
import com.ymttest.database.model.stockmgr.agentinventorysku;
import com.ymttest.database.model.stockmgr.agentinventoryskuExample;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryWapper;
import com.ymttest.database.sqlwapper.stockmgr.agentinventoryskuWapper;
import com.ymttest.utils.AgentUsers;
/**
*MQ调入库
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_mqStockOut{
	private static mqStockOutBean mqstockoutBean;
	private static mqStockOutCall mqstockoutCall;
	
	agentinventoryExample inventoryExample=new agentinventoryExample();
	agentinventoryWapper inventory=new agentinventoryWapper();
	
	agentinventoryskuExample skuExample=new agentinventoryskuExample();
	agentinventoryskuWapper sku=new agentinventoryskuWapper();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("MQ调入库");
	}

	@Before
	public void caseUp() {
		mqstockoutBean=new mqStockOutBean();
		mqstockoutCall=new mqStockOutCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(mqstockoutCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	// 渠道商
		private static int rootUserId = AgentUsers.rootUserId;
		private static String rootUserName = "NickautoTest";
		// 渠道商->代理商1
		private static int agentUserId1 = AgentUsers.agentUserId1;
		private static String agentUserName1 = "NickautoTest";
		// vip渠道
		private static int vipRootUserId = AgentUsers.vipRootUserId;
		private static String vipRootUserName = "NickautoTest";
		private static int csId = AgentUsers.csId;
		// vip渠道->代理
		private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
		private static String vipAgentUserName1 = "dgsq408897016";
		private static int csId1 = AgentUsers.csId1;
		
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_001() {
		Logger.start(true,"正例-MQ调出库");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			Logger.comment("入库前的数量");
			inventoryExample.createCriteria().andSellerIdEqualTo(rootUserId);	
			List<agentinventory> inventorys1=inventory.selectByExample(inventoryExample);
			
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			mqstockoutBean.setSellerId(rootUserId);
			mqstockoutBean.setRemark("手动增加五件出库货");
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(productId);
			catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			catalog.setNum(10);

			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			//Logger.verifyEquals("200", mqstockoutCall.getString("code"), "验证code");
			Logger.verifyEquals("ok", mqstockoutCall.getReturnData(), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock()-10,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock()-10,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
			//新增验证 wuwen 20200210
			inventoryExample.createCriteria().andSellerIdEqualTo(rootUserId);
			inventoryExample.setOrderByClause(" id desc ");
			skuExample.createCriteria().andProductIdEqualTo(productId).andCatalogIdEqualTo(productDetailsDto.getCatalogList().get(0).getCatalogId());
			List<agentinventorysku> skus=sku.selectByExample(skuExample);
			List<agentinventory> inventorys2=inventory.selectByExample(inventoryExample);
			Logger.verifyEquals(1, skus.size(), "验证agentinventorysku中新增一条数据");
			Logger.verifyEquals(1, inventorys2.size()-inventorys1.size(), "验证agentinventory中新增一条数据");
			
			agentinventorysku sku=skus.get(0);
			agentinventory inventory=inventorys2.get(0); 
			Logger.verifyEquals(10,sku.getNum()*-1, "验证agentinventorysku中的库存");
			Logger.verifyEquals(2,inventory.getType(), "验证agentinventory中的type=2手动出库");
			Logger.verifyNotNull(inventory.getInventoryNo(), "验证agentinventory中的InventoryNo单号");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_002() {
		Logger.start(true,"代理商增加出库单，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(6);
			addAgentProductBean.getCatalogList().get(0).setStock(5);
			String RootproductId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId1, vipAgentUserName1, RootproductId, vipRootUserId, false);
			String productId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(vipAgentUserId1, productId);
			mqstockoutBean.setSellerId(rootUserId);
			mqstockoutBean.setRemark("手动增加五件货");
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(productId);
			catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			catalog.setNum(10);
			
			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			//Logger.verifyEquals("500", mqstockoutCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mqstockoutCall.getReturnData().contains("系统处理失败|库存服务扣库存失败商品库存不足|商品库存不足"), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(vipAgentUserId1, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_003() {
		Logger.start(true,"渠道商手动增加小于0的库存，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			mqstockoutBean.setSellerId(rootUserId);
			mqstockoutBean.setRemark("手动增加五件货");
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(productId);
			catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			catalog.setNum(-10);
			
			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			//Logger.verifyEquals("400", mqstockoutCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mqstockoutCall.getReturnData().contains("无效的请求参数|数量必须大于0"), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_004() {
		Logger.start(true,"用户id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			mqstockoutBean.setSellerId(0);
			mqstockoutBean.setRemark("手动增加五件货");
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(productId);
			catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			catalog.setNum(10);
			
			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			//Logger.verifyEquals("400", mqstockoutCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mqstockoutCall.getReturnData().contains("无效的请求参数|商户id必填"), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_005() {
		Logger.start(true,"商品id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			mqstockoutBean.setSellerId(rootUserId);
			mqstockoutBean.setRemark("手动增加五件货");
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(null);
			catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			catalog.setNum(10);
			
			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			//Logger.verifyEquals("400", mqstockoutCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mqstockoutCall.getReturnData().contains("无效的请求参数|商品id不能为空"), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_006() {
		Logger.start(true,"规格id为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			mqstockoutBean.setSellerId(rootUserId);
			mqstockoutBean.setRemark("手动增加五件货");
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(productId);
			catalog.setCatalogId(null);
			catalog.setNum(10);
			
			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			//Logger.verifyEquals("400", mqstockoutCall.getString("code"), "验证code");
			Logger.verifyEquals(true, mqstockoutCall.getReturnData().contains("无效的请求参数|规格id不能为空"), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_008() {
		Logger.start(true,"没加描述，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			mqstockoutBean.setSellerId(rootUserId);
			mqstockoutBean.setRemark(null);
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(productId);
			catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			catalog.setNum(10);
			
			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			//Logger.verifyEquals("ok", mqstockoutCall.getReturnData(), "验证返回值");
			Logger.verifyEquals("ok", mqstockoutCall.getReturnData(), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock()-10,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock()-10,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_mqStockOut_009() {
		Logger.start(true,"渠道商增加出库单-1调整库存，成功");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			Logger.comment("入库前的数量");
			inventoryExample.createCriteria().andSellerIdEqualTo(rootUserId);	
			List<agentinventory> inventorys1=inventory.selectByExample(inventoryExample);
			
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			mqstockoutBean.setSellerId(rootUserId);
			mqstockoutBean.setRemark("手动增加五件出库货");
			CatalogDto catalog=new CatalogDto();
			catalog.setProductId(productId);
			catalog.setCatalogId(productDetailsDto.getCatalogList().get(0).getCatalogId());
			catalog.setNum(10);
			
			mqstockoutBean.setType(1);//1调整库存 新增业务 wuwen 20200211
			mqstockoutBean.setCatalogList(Arrays.asList(catalog));
			mqstockoutCall.setData(mqstockoutBean);
			mqstockoutCall.callService();			
			Logger.verifyEquals("ok", mqstockoutCall.getReturnData(), "验证返回值");
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			//调整入库 库存不变 wuwen 20200211
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getStock(),productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(productDetailsDto.getCatalogList().get(0).getRealStock(),productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
		
			//新增验证 wuwen 20200210
			inventoryExample.createCriteria().andSellerIdEqualTo(rootUserId);
			inventoryExample.setOrderByClause(" id desc ");
			skuExample.createCriteria().andProductIdEqualTo(productId).andCatalogIdEqualTo(productDetailsDto.getCatalogList().get(0).getCatalogId());
			List<agentinventorysku> skus=sku.selectByExample(skuExample);
			List<agentinventory> inventorys2=inventory.selectByExample(inventoryExample);
			Logger.verifyEquals(1, skus.size(), "验证agentinventorysku中新增一条数据");
			Logger.verifyEquals(1, inventorys2.size()-inventorys1.size(), "验证agentinventory中新增一条数据");
			
			agentinventorysku sku=skus.get(0);
			agentinventory inventory=inventorys2.get(0); 
			Logger.verifyEquals(10,sku.getNum()*-1, "验证agentinventorysku中的库存");
			//清单类型，1手动入库，2手动出库，3发货出库，4调整入库，5调整出库
			Logger.verifyEquals(5,inventory.getType(), "验证agentinventory中的type=5调整出库");
			Logger.verifyNotNull(inventory.getInventoryNo(), "验证agentinventory中的InventoryNo单号");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}